package com.adamrocker.android.gorillamd;

import java.util.List;
import twitter4j.Tweet;
import com.adamrocker.android.gorilla.twitter.TwitterSearch;
import com.adamrocker.android.gorilla.util.Kanji2RomaByWeb;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

public class GorillaReceiver extends BroadcastReceiver {

    private List<Tweet> mTweets;

    @Override
    public void onReceive(Context context, Intent intent) {
        String hash = GorillaMDActivity.loadHashtag(context);
        try { 
            Toast.makeText(context, "GORILLA RECEIVER", Toast.LENGTH_LONG).show();
            mTweets = TwitterSearch.getTweets(hash, 100);
            String text = mTweets.get(0).getText();
            Log.i("GORILLA", text);
            Toast.makeText(context, text, Toast.LENGTH_LONG).show();
            String roman = getRoman(text);
            
            Intent it = new Intent(Intent.ACTION_SEND);
            it.setData(Uri.parse("gorilla://"));
            it.putExtra("gori_message", roman);
            context.startActivity(it);
        } catch (Exception e){
            e.printStackTrace();
            Toast.makeText(context, "ERROR: GORILLA RECEIVR", Toast.LENGTH_LONG).show();
        }
    }
    
    private String getRoman(String text) {
        Kanji2RomaByWeb kanji = new Kanji2RomaByWeb();
        String roman = kanji.getRoman(text);
        return roman;
    }
}
